package com.kunya.common.common;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;

import java.io.Serializable;

/**   
* @author zp  
* @date 2018年4月15日  
*/
//序列化json时，去除value为null的key
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ServerResponse<T> implements Serializable {
	
	private int status;
	private String msg;
	private T data;
	
    public ServerResponse(){

	}
	public ServerResponse(int status) {
		super();
		this.status = status;
	}
	public ServerResponse(int status, T data) {
		this.status = status;
		this.data = data;
	}
	public ServerResponse(int status, String msg, T data) {
		super();
		this.status = status;
		this.msg = msg;
		this.data = data;
	}
	public ServerResponse(int status, String msg) {
		super();
		this.status = status;
		this.msg = msg;
	}
	@JsonIgnore
    //让其不在json序列化结果中
	public boolean isSuccess() {
	   return this.status==ResponseCode.SUCCESS.getCode();
	}
	public int getStatus() {
		return status;
	}
	public String getMsg() {
		return msg;
	}
	public T getData() {
		return data;
	}
	//返回成功
	public static <T>ServerResponse<T> createBySuccess() {
		return new ServerResponse<T>(ResponseCode.SUCCESS.getCode());
	}
	public static <T>ServerResponse<T> createBySuccessMessage(String msg) {
		return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),msg);
	}
	public static <T>ServerResponse<T> createBySuccess(T data) {
		return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),data);
	}
	public static <T> ServerResponse<T> createBySuccess(String msg,T data){
	        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),msg,data);
	    }
	//返回失败
	public static <T>ServerResponse<T> createByError() {
		return new ServerResponse<T>(ResponseCode.ERROR.getCode(),ResponseCode.ERROR.getDesc());
	}
	public static <T>ServerResponse<T> createByErrorMessage(String errorMsg) {
		return new ServerResponse<T>(ResponseCode.ERROR.getCode(),errorMsg);
	}
	public static <T>ServerResponse<T> createByErrorCodeMessage(int errorcode,String errormessage) {
		return new ServerResponse<T>(errorcode,errormessage);
	}
}
